﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace BaGiaDanhCuop
{
    public partial class HelpPage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        UIElementRendererHelper uiRenderer;

        Texture2D Touch_Help_TT;
        Texture2D Woman_Move_TT, Woman_Fighting_Mid_TT;
        Texture2D Batoong_Move_TT, Batoong_Mid_TT;
        Texture2D Cop_TT;
        Texture2D Power_BG_TT, Power_TT;
        Texture2D Guy_TT, Tree_TT, Post_TT;
        Texture2D Money_TT;

        Rectangle PowerRectangle;

        Animation Touch_Help_Ani;
        Animation Woman_Move_Ani, Woman_Fighting_Mid_Ani;
        Animation Batoong_Move_Ani, Batoong_Mid_Ani;
        Animation Cop_Ani;
        Animation Guy_Ani, Tree_Ani, Post_Ani;

        SpriteFont Help_Font;

        int touch_count = 0;

        // Constructor
        public HelpPage()
        {
            InitializeComponent();
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromMilliseconds(5);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;

            touch_count = 0;
          

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
            uiRenderer = new UIElementRendererHelper(this);
            

            timer.Start();
            LoadContent();
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();
            
            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
            uiRenderer.Dispose();
            base.OnNavigatedFrom(e);
        }

        //checktouch
        public void checkTouch()
        {
            TouchCollection touch = TouchPanel.GetState();
            foreach (TouchLocation tch in touch)
            {
                CheckButton(tch);
            }
        }

        public void CheckButton(TouchLocation touch)
        {
            if (touch.State == TouchLocationState.Pressed)
            {
                double x = touch.Position.X;
                double y = touch.Position.Y;
                if (touch_count < 3)
                {
                    touch_count++;
                }
            }
        }

        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            checkTouch();
            double delta = e.ElapsedTime.TotalSeconds;
            Woman_Move_Ani.Update(delta);
            Woman_Fighting_Mid_Ani.Update(delta);
            Batoong_Move_Ani.Update(delta);
            Batoong_Mid_Ani.Update(delta);
            Touch_Help_Ani.Update(delta);
            Tree_Ani.Update(delta);
            Cop_Ani.Update(delta);
            Guy_Ani.Update(delta);
            if (PowerRectangle.Width < Power_TT.Width)
                PowerRectangle.Width += 1;
            else PowerRectangle.Width = 0;
        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
            uiRenderer.PreRender();
            uiRenderer.Draw();

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            switch (touch_count)
            {
                case 0:
                    Woman_Move_Ani.Draw(spriteBatch, SpriteEffects.None);
                    Batoong_Move_Ani.Draw(spriteBatch, SpriteEffects.None);
                    Touch_Help_Ani.Draw(spriteBatch, SpriteEffects.None);
                    spriteBatch.DrawString(Help_Font, "Click, hold finger to increase power!", new Vector2(190, 200), Color.White);
                    spriteBatch.DrawString(Help_Font, "Then, release finger to hit!", new Vector2(220, 240), Color.White);
                    spriteBatch.Draw(Power_BG_TT, new Vector2(300, 350), Color.White);
                    spriteBatch.Draw(Power_TT, new Vector2(300, 350), PowerRectangle, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0);
                    break;
                case 1:
                    Woman_Fighting_Mid_Ani.Draw(spriteBatch, SpriteEffects.None);
                    Batoong_Mid_Ani.Draw(spriteBatch, SpriteEffects.None);
                    spriteBatch.DrawString(Help_Font, "Destroy everything...", new Vector2(250, 50), Color.White);
                    Guy_Ani.Draw(spriteBatch, SpriteEffects.None);
                    Tree_Ani.Draw(spriteBatch, 0.6f, SpriteEffects.None);
                    Post_Ani.Draw(spriteBatch, 0.8f, SpriteEffects.None);
                    spriteBatch.DrawString(Help_Font, "to claim back money!", new Vector2(250, 380), Color.White);
                    spriteBatch.Draw(Money_TT, new Vector2(550, 350), new Rectangle(0, 0, Money_TT.Width, Money_TT.Height), Color.White, 0f, Vector2.Zero, 2.5f, SpriteEffects.None, 0);
                    break;
                case 2:
                    Woman_Move_Ani.Draw(spriteBatch, SpriteEffects.None);
                    Batoong_Move_Ani.Draw(spriteBatch, SpriteEffects.None);
                    spriteBatch.DrawString(Help_Font, "Get away the Cop", new Vector2(270, 200), Color.White);
                    spriteBatch.DrawString(Help_Font, "if you don't want trouble!", new Vector2(220, 230), Color.White);
                    Cop_Ani.Draw(spriteBatch, SpriteEffects.None);
                    break;
                case 3:
                    Woman_Move_Ani.Draw(spriteBatch, SpriteEffects.None);
                    Batoong_Move_Ani.Draw(spriteBatch, SpriteEffects.None);
                    Woman_Move_Ani.Offset.X += 5;
                    Batoong_Move_Ani.Offset.X += 5;
                    if (Woman_Move_Ani.Offset.X > 800)
                    {
                        NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
                    }
                    break;
            }
            
            
            spriteBatch.End();
        }

        void LoadContent()
        {
            Help_Font = contentManager.Load<SpriteFont>("Font/HelpPage");

            Touch_Help_TT           = contentManager.Load<Texture2D>("Asset/Touch_Help");
            Woman_Move_TT           = contentManager.Load<Texture2D>("Asset/Woman_Move_TT");
            Woman_Fighting_Mid_TT   = contentManager.Load<Texture2D>("Asset/Woman_Fighting_Mid_TT");
            Batoong_Move_TT         = contentManager.Load<Texture2D>("Asset/Batoong_Move_TT");
            Batoong_Mid_TT          = contentManager.Load<Texture2D>("Asset/Batoong_Fighting_Mid_TT");
            Power_BG_TT             = contentManager.Load<Texture2D>("Asset/Skill_Power_Background");
            Power_TT                = contentManager.Load<Texture2D>("Asset/Skill_Power");
            Cop_TT                  = contentManager.Load<Texture2D>("Asset/Police");
            Guy_TT                  = contentManager.Load<Texture2D>("Asset/Guy01_Move");
            Tree_TT                 = contentManager.Load<Texture2D>("Asset/Tree");
            Post_TT                 = contentManager.Load<Texture2D>("Asset/Tele_Box");
            Money_TT                = contentManager.Load<Texture2D>("Asset/Money_TT");

            PowerRectangle = new Rectangle(0, 0, 0, Power_TT.Height);

            Touch_Help_Ani = new Animation(new Vector2(500, 300), Touch_Help_TT, new Microsoft.Xna.Framework.Point(6, 1));
            Touch_Help_Ani.animate(0.125f);

            Woman_Move_Ani = new Animation(new Vector2(60, 100), Woman_Move_TT, new Microsoft.Xna.Framework.Point(5, 2));
            Woman_Move_Ani.animate(0.125f);

            Batoong_Move_Ani = new Animation(new Vector2(60, 100), Batoong_Move_TT, new Microsoft.Xna.Framework.Point(5, 2));
            Batoong_Move_Ani.animate(0.125f);

            Woman_Fighting_Mid_Ani = new Animation(new Vector2(60, 100), Woman_Fighting_Mid_TT, new Microsoft.Xna.Framework.Point(6, 1));
            Woman_Fighting_Mid_Ani.animate(0.125f);

            Batoong_Mid_Ani = new Animation(new Vector2(60, 100), Batoong_Mid_TT, new Microsoft.Xna.Framework.Point(6, 1));
            Batoong_Mid_Ani.animate(0.125f);

            Cop_Ani = new Animation(new Vector2(570, 100), Cop_TT, new Microsoft.Xna.Framework.Point(6, 1));
            Cop_Ani.animate(0.25f);

            Guy_Ani = new Animation(new Vector2(350, 100), Guy_TT, new Microsoft.Xna.Framework.Point(6, 1));
            Guy_Ani.animate(0.2f);
            Tree_Ani = new Animation(new Vector2(200, 100), Tree_TT, new Microsoft.Xna.Framework.Point(6, 1));
            Tree_Ani.animate(0.125f);
            Post_Ani = new Animation(new Vector2(500, 100), Post_TT, new Microsoft.Xna.Framework.Point(5, 1));
            Post_Ani.animate(0.25f);
        }
    }
}